﻿using Abp.Application.Services.Dto;
using Abp.Web.Mvc.Authorization;
using Akso.Authorization;
using Akso.Codes;
using Akso.DataMaintains;
using Akso.Enums;
using Akso.Schools;
using Akso.Web.Areas.FinanceModule.Models.DataMaintains;
using Akso.Web.Controllers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;

namespace Akso.Web.Areas.FinanceModule.Controllers
{
    [AbpMvcAuthorize(AppPermissions.Pages_FinanceModule_DataMaintains)]
    public class DataMaintainsController : AbpZeroTemplateControllerBase
    {
        // GET: FinanceModule/DataMaintains
        private readonly IDataMaintainAppService _dataMaintainsAppService;
        private readonly ISchoolAppService _schoolsAppService;
        private readonly ICodeAppService _codesAppService;
        public DataMaintainsController(IDataMaintainAppService dataMaintainsAppService, ISchoolAppService schoolsAppService, ICodeAppService codesAppService)
        {
            _dataMaintainsAppService = dataMaintainsAppService;
            _schoolsAppService = schoolsAppService;
            _codesAppService = codesAppService;
        }
        public ActionResult Index()
        {
            var model = new DataMaintainsViewModel
            {
                FilterText = Request.QueryString["filterText"]
            };
            return View(model);
        }
        #region 新增编辑账户
        [AbpMvcAuthorize(AppPermissions.Pages_FinanceModule_DataMaintains_CreateAccount, AppPermissions.Pages_FinanceModule_DataMaintains_EditAccount)]
        public async Task<PartialViewResult> CreateOrEditBillAccountModal(Guid? id)
        {
            var output = await _dataMaintainsAppService.GetBillAccountForEdit(new NullableIdDto<Guid> { Id = id });
            var schoolList = _schoolsAppService.GetAllSchools().Items.Select(p => new ComboboxItemDto(p.Id.ToString(), p.SchoolName)).ToList();
            schoolList.Insert(0, new ComboboxItemDto("", ""));
            if (id != null)
            {
                foreach (var item in schoolList)
                {
                    if (item.Value == output.BillAccount.SchoolId.ToString())
                        item.IsSelected = true;
                    else
                        item.IsSelected = false;
                }
            }
            var viewModel = new CreateOrEditBillAccountModalViewModel(output);
            viewModel.GetSchoolList = schoolList;

            return PartialView("_CreateOrEditBillAccountModal", viewModel);
        }
        #endregion

        #region 新增编辑收支分类
        [AbpMvcAuthorize(AppPermissions.Pages_FinanceModule_DataMaintains_CreateIEType, AppPermissions.Pages_FinanceModule_DataMaintains_EditIEType)]
        public async Task<PartialViewResult> CreateOrEditIncomeExpendTypeModal(Guid? id)
        {
            var output = await _dataMaintainsAppService.GetIncomeExpendTypeForEdit(new NullableIdDto<Guid> { Id = id });
            
            var viewModel = new CreateOrEditIncomeExpendTypeModalViewModel(output);
         

            return PartialView("_CreateOrEditIncomeExpendTypeModal", viewModel);
        }
        #endregion

        #region 新增编辑辅助核算
        /// <summary>
        /// 新增编辑辅助核算
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [AbpMvcAuthorize(AppPermissions.Pages_FinanceModule_DataMaintains_CreateOtherCheck, AppPermissions.Pages_FinanceModule_DataMaintains_EditOtherCheck)]
        public async Task<PartialViewResult> CreateOrEditOtherCheckModal(Guid? id)
        {
            var output = await _dataMaintainsAppService.GetBillOtherCheckForEdit(new NullableIdDto<Guid> { Id = id });
            var checktypeList = _codesAppService.GetAllCodesByGroupKey(EnumCollection.EnumCode.OTHERCHECK_TYPE.ToString()).Items.Select(p => new ComboboxItemDto(p.CodeKey, p.CodeName)).ToList();
            //checktypeList.Insert(0, new ComboboxItemDto("", ""));
            if(id!=null)
            {
                foreach (var item in checktypeList)
                {
                    if (item.Value == output.OtherCheck.CheckTypeId.ToString())
                        item.IsSelected = true;
                    else
                        item.IsSelected = false;
                }
            }
            var viewModel = new CreateOrEditOtherCheckModalViewModel(output);
            viewModel.CheckType = output.OtherCheck.CheckType;
            viewModel.GetCheckTypeList = checktypeList;
            return PartialView("_CreateOrEditOtherCheckModal", viewModel);
        }

        
        #endregion
    }
}